var map;

function showCurrentPosition() {
    console.log("Called showCurrentPosition()");
    
    // Try HTML5 geolocation
    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(geolocationSuccess, handleNoGeolocation, {timeout: 3000, enableHighAccuracy: true, maximumAge: 75000});
    }
}

function geolocationSuccess(p){
    $('#Latitude').text("Latitude: " + p.coords.latitude);
    $('#Longitude').text("Longitude: " + p.coords.longitude);
    
    var pos = new google.maps.LatLng(p.coords.latitude,
                                     p.coords.longitude);
    
    var mapOptions = {
        center: pos,
        zoom: 13,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

map = new google.maps.Map(document.getElementById('geomap'),
                          mapOptions);


var marker = new google.maps.Marker({
                                    position: pos,
                                    title:"Your Position"
                                    });

// To add the marker to the map, call setMap();
marker.setMap(map);

map.setCenter(pos);
}

function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
    } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
    }
    
    console.log("Error" + errorFlag.message);
    
    var options = {
    map: map,
    position: new google.maps.LatLng(60, 105),
    content: content
    };
    
    var infowindow = new google.maps.InfoWindow(options);
    map.setCenter(options.position);
}